package com.token.model.dto;

import com.token.service.Predicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: henriy
 * @Date: 2023/12/29
 */
public class PredicateTest {

    private final Predicate<String> predicate;

    public PredicateTest(Predicate<String> predicate) {
        this.predicate = predicate;
    }

    public boolean test(String str) {
        return predicate.test(str);
    }

    public static void main(String[] args) {
        PredicateTest example = new PredicateTest(String::isEmpty);
        // true
        System.out.println(example.test(""));
        // false
        System.out.println(example.test("Hello"));

        // 过滤出集合中，字符串长度为 4 的字符串。
        List<String> list = Arrays.asList("java", "node", "www.wdbyte.com");
        list = list.stream()
                .filter(str -> str.length() == 4)
                .collect(Collectors.toList());
        System.out.println(list);

        // 过滤数字集合中，数字大小在 5 至 9 之间的数字。
        List<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10);
        numberList = numberList.stream()
                .filter(x -> x > 5 && x < 9)
                .collect(Collectors.toList());
        System.out.println(numberList);

        // 过滤数字集合中，数字不大于 5 的数字。
        List<Integer> numberList1 = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10);
        numberList1 = numberList1.stream()
                .filter(number -> number > 5)
                .collect(Collectors.toList());
        System.out.println(numberList1);

        // 过滤数字集合中，数字小于等于 5，或者大于等于 9 的数字。
        List<Integer> numberList2 = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10);
        numberList2 = numberList2.stream()
                .filter(number -> number <= 5 | number >= 9)
                .collect(Collectors.toList());
        System.out.println(numberList2);

        // Predicate 的 or() ，and()，negate() 方法组合使用。
        List<Integer> numberList3 = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10);
        Predicate<Integer> lessThan5 = number -> number <= 5;
        Predicate<Integer> greaterThan9 = number -> number >= 9;

        // 小于等于 5
        System.out.println(filter(numberList3, lessThan5));
        // 大于 5
        System.out.println(filter(numberList3, lessThan5.negate()));
        // 小于等于 5 或者大于等于 9
        System.out.println(filter(numberList3, lessThan5.or(greaterThan9)));
        // ! (小于等于 5 AND 大于等于 9)
        System.out.println(filter(numberList3, lessThan5.and(greaterThan9).negate()));

        List<Dog> dogList = new ArrayList<>();
        dogList.add(new Dog("哈士奇", 1));
        dogList.add(new Dog("牧羊犬", 2));
        dogList.add(new Dog("柯基", 3));
        dogList.add(new Dog("柴犬", 3));

        // 找到 3岁的狗
        System.out.println(filter(dogList, dog -> dog.getAge().equals(3)));
        // 找到哈士奇信息
        Predicate<Dog> predicate = dog -> ("哈士奇").equals(dog.getName());
        System.out.println(filter(dogList, predicate));
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
        List<T> resultList = new ArrayList<>();
        for (T t : list) {
            if (predicate.test(t)) {
                resultList.add(t);
            }
        }
        return resultList;
    }

}
